import axios from 'axios'
import qs from 'qs'

export default {
    namespaced: true,
    actions: {
        getPlate: function ({commit}, params) {
            let {callback} = params;
            params.onget = ({data}) => {
                callback(data);
            }
            commit("getPlate", params);
        },
        getAllPlate: function ({commit}, params) {
            let {callback} = params;
            params.onget = ({data}) => {
                callback(data);
            }
            commit("getAllPlate", params);
        },
        addPlate: function ({commit}, params) {
            let {callback} = params;
            params.onget = ({data}) => {
                callback(data);
            }
            commit("addPlate", params);
        },
        updatePlate: function ({commit}, params) {
            let {callback} = params;
            params.onget = ({data}) => {
                callback(data);
            }
            commit("updatePlate", params);
        },
        deletePlate: function ({commit}, params) {
            let {callback} = params;
            params.onget = ({data}) => {
                callback(data);
            }
            commit("deletePlate", params);
        },
    },
    mutations: {
        getPlate: function (state, {id, onget}) {
            axios.get(`/admin/plate/${id}`).then(data => {
                onget(data);
            })
        },
        getAllPlate: function (state, {page,size,categoryId,name,view,onget}) {
            axios.get(`/admin/plate`, {
                params: {
                    categoryId,
                    name,
                    view,
                    page,size
                }
            }).then(data => {
                onget(data);
            })
        },
        addPlate: function (state, { categoryId,name,view,onget}) {
            axios.post(`/admin/plate`, qs.stringify({
                categoryId,name,view
            })).then(data => {
                onget(data);
            })
        },
        updatePlate: function (state, {id, categoryId,name,view, onget}) {
            axios.put(`/admin/plate`, qs.stringify({
                id, categoryId,name,view,
            })).then(data => {
                onget(data);
            })
        },
        deletePlate: function (state, {id, onget}) {
            axios.delete(`/admin/plate`,qs.stringify({
                id
            })).then(data => {
                onget(data);
            })
        },
    }

}